package servlet;

import domain.Product;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/2/28 8:25
 */
@WebServlet(name = "AddProductServlet",urlPatterns = "/addProduct")
public class AddProductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

             doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Product product = new Product();

        /*创建工厂*/
        DiskFileItemFactory factory = new DiskFileItemFactory();
        /*创建文件解析工具*/
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            /*对request对象进行解析，得到FileItem的集合*/
            List<FileItem> fileItems = upload.parseRequest(request);

            for(FileItem item :fileItems){
                //普通表单数据（只有两类，普通表单和文件）
                if (item.isFormField()){
                    //如果为pname，则将其设置到product的pname属性中
                    if (item.getFieldName().equals("pname")){
                        String value = item.getString();
                        product.setPname(value);
                    }
                    if (item.getFieldName().equals("price")){
                        product.setMarketprice(Double.parseDouble(item.getString()));
                    }
                    //...
                }else { //文件
                    String fileName = item.getName();
                    System.out.println(fileName);
                    //文件存哪里？怎么存？？
                    /**
                     * 数据库中存的是文件路径，真正的文件在磁盘存在
                     * product/5/....
                     * */

                    String path = request.getServletContext().getRealPath("/products/5");
                    File file = new File(path);
                    //当此文件夹不存在的时候，创建此文件夹
                    if (!file.exists()){
                        file.mkdirs();
                    }
                    //创建文件，
                    File f = new File(path,fileName);
                    if (!f.exists()){
                        f.createNewFile();
                    }

                    //读取的文件流写入此文件
                    item.write(f);

                    //设置产品的图片属性，也就是数据库中要存入的路径
                    product.setImage("product/5/"+fileName);

                    //如果传的文件名称在磁盘上已经存在了，怎么处理？？ ----》在文件名前加个UUID区别

                }
            }


        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
